import {IAppOption} from '../../app.option'
import {getWxUserProfile} from '../../utils/wx.api'
import {TripService} from '../../service/trip'
import {routing} from '../../constant/routing.constant'

const shareLocationKey = 'share_location'
const app = getApp<IAppOption>()

Page({
    carId: '',
    /**
     * 页面的初始数据
     */
    data: {
        avatarURL: '',
        shareLocation: false
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(opt: Record<'car_id', string>) {

        this.carId = opt.car_id

        const userInfo = await app.globalData.userInfo

        this.setData({
            avatarURL: userInfo.avatarUrl,
            shareLocation: wx.getStorageSync(shareLocationKey) || false,
        })

    },
    /**
     * 点击获取用户信息
     */
    async onGetUserInfo() {

        const userProfileResp = await getWxUserProfile()

        let userInfo = userProfileResp.userInfo

        if (userInfo) {

            app.resolveUserInfo(userInfo)

            this.setData({
                shareLocation: true,
                avatarURL: userInfo.avatarUrl
            })

            wx.setStorageSync(shareLocationKey, true)

        }
    },
    /**
     * 分享位置
     */
    onShareLocation(e: any) {
        this.data.shareLocation = e.detail.value
        wx.setStorageSync(shareLocationKey, this.data.shareLocation)
    },
    /**
     * 立即开锁
     */
    async onUnlockTap() {

        wx.getLocation({
            type: 'gcj02',
            success: async location => {

                // console.log('开始行车: ', {
                //     location: {
                //         longitude: location.longitude,
                //         latitude: location.latitude
                //     },
                //     // TODO
                //     avatarURL: this.data.shareLocation ? this.data.avatarURL : '',
                // })

                if (!this.carId) {
                    console.error('没有解析到carId')
                    return
                }

                await wx.showLoading({
                    title: '开锁中',
                    mask: true,
                })

                // TODO 创建出行
                const tripEntity = await TripService.CreateTrip({
                    start: {
                        longitude: location.longitude,
                        latitude: location.latitude
                    },
                    carId: this.carId
                })

                if (!tripEntity.id) {
                    console.error('没有返回tripId')
                    return
                }

                setTimeout(() => {

                    wx.redirectTo({
                        // url: `${routing.driving()}?trip_id=${tripId}`,
                        url: routing.driving({trip_id: tripEntity.id}),
                        complete: () => {
                            wx.hideLoading()
                        }
                    })

                }, 2000)

            },
            fail: () => {
                wx.showToast({
                    icon: 'none',
                    title: '请前往设置页授权位置信息',
                })
            }
        })

    }

})
